﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using YunSpace.Data;
using YunSpace.Data.Extensions;
using YunSpace.Domain.Entity.WorkFlow;
using YunSpace.Domain.IRepository.WorkFlow;
#endregion

namespace YunSpace.Repository.WorkFlow
{
    public class FlowDesignerRepository : IFlowDesignerRepository
    {
        private MySqlDbHelper dbHelper = new MySqlDbHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public FlowDesignerRepository()
        {
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">FlowDesignerEntity实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(FlowDesignerEntity model)
        {
            string sql = @"INSERT INTO WorkFlow_FlowDesigner
                (F_Id, F_Name, F_Type, F_Manager, F_InstanceManager, F_CreateDate, F_CreatorUserId, F_DesignJSON, F_InstallDate, F_InstallUserId, F_RunJSON, F_Status) 
				VALUES(@ID,@Name,@Type,@Manager,@InstanceManager,@CreateDate,@CreateUserID,@DesignJSON,@InstallDate,@InstallUserID,@RunJSON,@Status)";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = model.F_Id },
                new MySqlParameter("@Name", MySqlDbType.VarChar, 1000){ Value = model.F_Name },
                new MySqlParameter("@Type", MySqlDbType.VarChar, 50){ Value = model.F_Type },
                new MySqlParameter("@Manager", MySqlDbType.VarChar, 5000){ Value = model.F_Manager },
                new MySqlParameter("@InstanceManager", MySqlDbType.VarChar, 5000){ Value = model.F_InstanceManager },
                new MySqlParameter("@CreateDate", MySqlDbType.DateTime, 8){ Value = model.F_CreateDate },
                new MySqlParameter("@CreateUserID", MySqlDbType.VarChar, 50){ Value = model.F_CreateUserID },
                model.F_DesignJSON == null ? new MySqlParameter("@DesignJSON", MySqlDbType.VarChar, -1) { Value = DBNull.Value } : new MySqlParameter("@DesignJSON", MySqlDbType.VarChar, -1) { Value = model.F_DesignJSON },
                model.F_InstallDate == null ? new MySqlParameter("@InstallDate", MySqlDbType.DateTime, 8) { Value = DBNull.Value } : new MySqlParameter("@InstallDate", MySqlDbType.DateTime, 8) { Value = model.F_InstallDate },
                model.F_InstallUserID == null ? new MySqlParameter("@InstallUserID", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@InstallUserID", MySqlDbType.VarChar, 50) { Value = model.F_InstallUserID },
                model.F_RunJSON == null ? new MySqlParameter("@RunJSON", MySqlDbType.VarChar, -1) { Value = DBNull.Value } : new MySqlParameter("@RunJSON", MySqlDbType.VarChar, -1) { Value = model.F_RunJSON },
                new MySqlParameter("@Status", MySqlDbType.Int32){ Value = model.F_Status }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">FlowDesignerEntity实体类</param>
        public int Update(FlowDesignerEntity model)
        {
            string sql = @"UPDATE WorkFlow_FlowDesigner SET 
				F_Name=@Name,F_Type=@Type,F_Manager=@Manager,F_InstanceManager=@InstanceManager,F_CreateDate=@CreateDate,F_CreateUserID=@CreateUserID,F_DesignJSON=@DesignJSON,F_InstallDate=@InstallDate,F_InstallUserId=@InstallUserID,F_RunJSON=@RunJSON,F_Status=@Status
				WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@Name", MySqlDbType.VarChar, 1000){ Value = model.F_Name },
                new MySqlParameter("@Type", MySqlDbType.VarChar, 50){ Value = model.F_Type },
                new MySqlParameter("@Manager", MySqlDbType.VarChar, 5000){ Value = model.F_Manager },
                new MySqlParameter("@InstanceManager", MySqlDbType.VarChar, 5000){ Value = model.F_InstanceManager },
                new MySqlParameter("@CreateDate", MySqlDbType.DateTime, 8){ Value = model.F_CreateDate },
                new MySqlParameter("@CreateUserID", MySqlDbType.VarChar, 50){ Value = model.F_CreateUserID },
                model.F_DesignJSON == null ? new MySqlParameter("@DesignJSON", MySqlDbType.VarChar, -1) { Value = DBNull.Value } : new MySqlParameter("@DesignJSON", MySqlDbType.VarChar, -1) { Value = model.F_DesignJSON },
                model.F_InstallDate == null ? new MySqlParameter("@InstallDate", MySqlDbType.DateTime, 8) { Value = DBNull.Value } : new MySqlParameter("@InstallDate", MySqlDbType.DateTime, 8) { Value = model.F_InstallDate },
                model.F_InstallUserID == null ? new MySqlParameter("@InstallUserID", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@InstallUserID", MySqlDbType.VarChar, 50) { Value = model.F_InstallUserID },
                model.F_RunJSON == null ? new MySqlParameter("@RunJSON", MySqlDbType.VarChar, -1) { Value = DBNull.Value } : new MySqlParameter("@RunJSON", MySqlDbType.VarChar, -1) { Value = model.F_RunJSON },
                new MySqlParameter("@Status", MySqlDbType.Int32){ Value = model.F_Status },
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = model.F_Id }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(String id)
        {
            string sql = "DELETE FROM WorkFlow_FlowDesigner WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = id }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<FlowDesignerEntity> DataReaderToList(MySqlDataReader dataReader)
        {
            List<FlowDesignerEntity> List = new List<FlowDesignerEntity>();
            FlowDesignerEntity model = null;
            while (dataReader.Read())
            {
                model = new FlowDesignerEntity();
                model.F_Id = dataReader.GetString(0);
                model.F_Name = dataReader.GetString(1);
                model.F_Type = dataReader.GetString(2);
                model.F_Manager = dataReader.GetString(3);
                model.F_InstanceManager = dataReader.GetString(4);
                model.F_CreateDate = dataReader.GetDateTime(5);
                model.F_CreateUserID = dataReader.GetString(6);
                if (!dataReader.IsDBNull(7))
                    model.F_DesignJSON = dataReader.GetString(7);
                if (!dataReader.IsDBNull(8))
                    model.F_InstallDate = dataReader.GetDateTime(8);
                if (!dataReader.IsDBNull(9))
                    model.F_InstallUserID = dataReader.GetString(9);
                if (!dataReader.IsDBNull(10))
                    model.F_RunJSON = dataReader.GetString(10);
                model.F_Status = dataReader.GetInt32(11);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<FlowDesignerEntity> GetAll()
        {
            string sql = "SELECT * FROM WorkFlow_FlowDesigner";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<FlowDesignerEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM WorkFlow_FlowDesigner";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public FlowDesignerEntity Get(String id)
        {
            string sql = "SELECT * FROM WorkFlow_FlowDesigner WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = id }
            };
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<FlowDesignerEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 查询所有类型
        /// </summary>
        public List<string> GetAllTypes()
        {
            string sql = "SELECT F_Type FROM WorkFlow_FlowDesigner GROUP BY F_Type";
            List<string> list = new List<string>();
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            while (dataReader.Read())
            {
                list.Add(dataReader.GetString(0));
            }
            dataReader.Close();
            return list;
        }

        /// <summary>
        /// 查询所有ID和名称
        /// </summary>
        public Dictionary<String, string> GetAllIDAndName()
        {
            string sql = "SELECT F_Id, F_Name FROM WorkFlow_FlowDesigner WHERE F_Status<4 ORDER BY F_Name";
            Dictionary<String, string> dict = new Dictionary<String, string>();
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            while (dataReader.Read())
            {
                dict.Add(dataReader.GetString(0), dataReader.GetString(1));
            }
            dataReader.Close();
            return dict;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<FlowDesignerEntity> GetByTypes(string typeString)
        {
            string sql = "SELECT * FROM WorkFlow_FlowDesigner where F_Type IN(" + Utility.Tools.GetSqlInString(typeString) + ")";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<FlowDesignerEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}

